#include "serial_port.h"

SerialPort::SerialPort(QObject *parent) : QObject(parent)
{
    this->m_uart = new QSerialPort(this);
    connect(this->m_uart, SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this, SLOT(errorOccurred(QSerialPort::SerialPortError)));
    connect(this->m_uart, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

SerialPort::~SerialPort()
{
    this->m_uart->close();
    delete m_uart;
}

bool SerialPort::open()
{
    this->m_uart->setPortName(this->m_uartName);
    this->m_uart->setBaudRate(this->m_baud);
    this->m_uart->setParity(this->m_parity);
    this->m_uart->setDataBits(this->m_dataBits);
    this->m_uart->setStopBits(this->m_stopBits);
    if (this->m_uart->open(QIODevice::ReadWrite)){
        return true;
    }
    else {
        this->m_error = this->m_uart->errorString();
        return false;
    }
}

void SerialPort::close()
{
    this->m_uart->close();
}
